package com.aistar.controller;

import com.aistar.service.EvaluationService;
import com.aistar.service.UploadImgPicService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class EvaluationController {

    @Autowired
    private UploadImgPicService uploadImgPicService;

    @Autowired
    private EvaluationService evaluationService;

    private static int evaId = 0;

    @RequestMapping("/addEvaluation")
    private String order(){
        return "evaluation/evaluation_add";
    }

    @PostMapping("/evaluationAdd")
    @ResponseBody
    private ServerResponse EvaluationAdd(String cusid,String proid,String grade,String token,String etype,String edesc) {

        //1、接收前端发送过来的数据
        String CusIdStr = cusid;
        String ProIdStr = proid;
        String gradeStr = grade;
        String typeStr = etype;
        String descStr = edesc;
        String tokenStr = token;

        System.out.println("令牌 ： " + token);

        //数据类型转换
        Integer customerId = Integer.parseInt(CusIdStr.trim());
        Integer productId = Integer.parseInt(ProIdStr.trim());
        Integer star = Integer.parseInt(gradeStr.trim());

        ServerResponse serverResponse = evaluationService.addEvaluation(customerId,productId,star,token,typeStr,descStr);

        //取出
        return serverResponse;
    }

    @PostMapping("/evaluationImgUpload/{token}")
    @ResponseBody
    private ServerResponse EvaluationImgUpload(@PathVariable("token") String token,HttpServletRequest request, MultipartFile file) {

        System.out.println("添加图片的令牌" + token);
        ServerResponse serverResponse = uploadImgPicService.upload(file,token);
        return serverResponse;
    }

}
